package servlet;

import com.google.gson.Gson;
import dao.Impl.StaffsDaoImpl;
import dao.StaffsDao;
import entitl.Staffs;
import vo.ResponseData;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @Author czl
 * @Date 2024-03-07
 */
@WebServlet("/staffs_insert")
public class staffsInsert extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        int age = Integer.parseInt(req.getParameter("age"));
        String phone = req.getParameter("phone");
        String pos = req.getParameter("pos");

        Staffs staffs= new Staffs();
        staffs.setName(name);
        staffs.setAge(age);
        staffs.setPhone(phone);
        staffs.setPos(pos);

        StaffsDao dao = new StaffsDaoImpl();
        int row=dao.insert(staffs);

        ResponseData responseData = new ResponseData();
        responseData.setMsg("添加成功");

        if(row!=1) {
            responseData.setMsg("添加成功");
            responseData.setCode(500);
        }

        String json = new Gson().toJson(responseData);
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        out.print(json);

        out.flush();
        out.close();
    }
}
